import { Dictionary } from '@shencom/typing';
import axios from 'axios';

import { token } from './token';

export interface ScHeaders extends Dictionary {
  scid?: string;
  Authorization?: string | null;
}

axios.defaults.headers.common.scid = 'sc08f17c22b8de639f';
axios.defaults.headers.common.Authorization = token;
axios.defaults.timeout = 20000;

axios.interceptors.request.use(
  (config) => {
    console.log('发起请求', config.url, config.data, config.headers);
    return config;
  },
  (error) => {
    console.log('请求错误', error);
    return Promise.reject(error);
  },
);
axios.interceptors.response.use(
  (response) => {
    console.log(
      '响应请求',
      response.config.url,
      response.config.data,
      response.data,
    );
    return response;
  },
  (error) => {
    console.log('响应错误', error);
    return Promise.reject(error);
  },
);

class http {
  static post<T>(url: string, data?: Record<string, any>, headers?: ScHeaders) {
    return axios.request<T>({ url, data, method: 'post', headers });
  }

  static get<T>(
    url: string,
    params?: Record<string, any>,
    headers?: ScHeaders,
  ) {
    return axios.request<T>({ url, params, method: 'get', headers });
  }
}

export { http };
